#pragma once
#include <iostream>
#include <boost/asio.hpp>
#include <boost/uuid.hpp>
#include "MsgNode.h"
#include <boost/asio/co_spawn.hpp>
#include <queue>
#include <boost/asio/detached.hpp>
#include "CServer.h"
#include "LogicSys.h"

namespace this_coro = boost::asio::this_coro;

class CServer;
class CSession:public std::enable_shared_from_this<CSession>
{
public:
	CSession(boost::asio::io_context& ioc, CServer* server);
	boost::asio::ip::tcp::socket& getSocket();
	void start();
	std::string& getuuid();
	void Close();
	void HandleErr(std::string);
	~CSession();
	void Send(std::string msg, short msg_id);
	void Send(const char* msg, short msg_len,short msg_id);
	void HandleWrite(const boost::system::error_code&, std::shared_ptr<CSession>);
private:
	boost::asio::io_context& _ioc;
	CServer* _server;
	boost::asio::ip::tcp::socket _socket;
	std::string _uuid;
	bool _b_stop;

	std::mutex _send_mtx;
	std::queue<std::shared_ptr<SendNode>> _send_que;
	std::shared_ptr<MsgNode> _head_node;
	std::shared_ptr<RecvNode> _recv_node;
};
class LogicNode {
	friend class LogicSys;
public:
	LogicNode(std::shared_ptr<CSession>, std::shared_ptr<RecvNode>);
private:
	std::shared_ptr<CSession> _session;
	std::shared_ptr<RecvNode> _recvnode;
};
